package com.matrix.index.ui.home.fragment

import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.Manifest.permission.READ_MEDIA_IMAGES
import android.Manifest.permission.READ_MEDIA_VIDEO
import android.Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ToastUtils
import com.matrix.basecore.arouter.ARouterUtils
import com.matrix.basecore.base.BaseFragment
import com.matrix.basecore.constant.Constant
import com.matrix.index.databinding.IndexHomeFragmentBinding

class IndexHomeFragment(fragment: IndexFragment) : BaseFragment<IndexHomeFragmentBinding>() {

    private var fragment: IndexFragment = fragment

    private val permissionLauncher =
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
            checkPermission()
        }

    override fun getViewBinding(): IndexHomeFragmentBinding {
        return IndexHomeFragmentBinding.inflate(layoutInflater)
    }

    override fun initView(inflater: LayoutInflater) {

    }

    override fun initData() {

    }

    override fun initListener() {
        mViewBinding.rlEdit.setOnClickListener {
            ARouterUtils.startPictureEditActivity()
        }
        mViewBinding.rlGif.setOnClickListener {
            requestPermissions()
        }
        mViewBinding.rlDm.setOnClickListener {
            ARouterUtils.startMarqueeActivity()
        }

        mViewBinding.rlAi.setOnClickListener {
            ARouterUtils.startAiMenuActivity()
        }

        mViewBinding.rlJoke.setOnClickListener {
            fragment.selectViewpager(1)
        }

        mViewBinding.rlMap.setOnClickListener {
            fragment.selectViewpager(2)
        }

        mViewBinding.includeGame.cvGameFive.setOnClickListener {
            ARouterUtils.startGameActivity(Constant.Value.FIVE)
        }
        mViewBinding.includeGame.cvGameTouzi.setOnClickListener {
            ARouterUtils.startGameActivity(Constant.Value.TOU)
        }
        mViewBinding.includeGame.cvGamePlant.setOnClickListener {
            ARouterUtils.startGameActivity(Constant.Value.AIR)
        }
        mViewBinding.includeGame.cvGameSnack.setOnClickListener {
            ARouterUtils.startGameActivity(Constant.Value.SNACK)
        }
        mViewBinding.includeGame.cvGameCircle.setOnClickListener {
            ARouterUtils.startGameActivity(Constant.Value.CIRCLE)
        }
        mViewBinding.includeGame.cvGameChoose.setOnClickListener {
            ARouterUtils.startGameActivity(Constant.Value.CHOOSE)
        }
        mViewBinding.includeGame.cvGameShudu.setOnClickListener {
            ARouterUtils.startGameActivity(Constant.Value.SHUDU)
        }
    }

    private fun requestPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            permissionLauncher.launch(
                arrayOf(
                    READ_MEDIA_IMAGES,
                    READ_MEDIA_VIDEO,
                    WRITE_EXTERNAL_STORAGE,
                    READ_EXTERNAL_STORAGE
                )
            )
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.TIRAMISU) {
            permissionLauncher.launch(
                arrayOf(
                    READ_MEDIA_IMAGES,
                    READ_MEDIA_VIDEO,
                    WRITE_EXTERNAL_STORAGE,
                    READ_EXTERNAL_STORAGE
                )
            )
        } else {
            permissionLauncher.launch(arrayOf(READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE))
        }
    }

    private fun checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
            && (ContextCompat.checkSelfPermission(
                requireContext(),
                READ_MEDIA_IMAGES
            ) == PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(
                requireContext(),
                READ_MEDIA_VIDEO
            ) == PERMISSION_GRANTED)
        ) {
            // Full access on Android 13 (API level 33) or higher
            ARouterUtils.startGIFActivity()
        } else if (
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE &&
            ContextCompat.checkSelfPermission(
                requireContext(),
                READ_MEDIA_VISUAL_USER_SELECTED
            ) == PERMISSION_GRANTED
        ) {
            // Partial access on Android 14 (API level 34) or higher
            ARouterUtils.startGIFActivity()
        } else if (ContextCompat.checkSelfPermission(
                requireContext(),
                READ_EXTERNAL_STORAGE
            ) == PERMISSION_GRANTED
        ) {
            // Full access up to Android 12 (API level 32)
            ARouterUtils.startGIFActivity()
        } else {
            // Access denied
            ToastUtils.showShort("你还未授权访问存储")
        }
    }
}